﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.Web.WebControls;

namespace EPiServer.Municipality.Web.UI.WebControls {
    /// <summary>
    /// Summary description for PagedList.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class PagedList : PageListData {
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public ITemplate HeaderTemplate
        /// <summary>
        /// Get/Sets the HeaderTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate HeaderTemplate {
            get {
                return _headerTemplate;
            }
            set {
                _headerTemplate = value;
            }
        }
        private ITemplate _headerTemplate;
        #endregion
        #region public ITemplate ItemTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate ItemTemplate {
            get {
                return _itemTemplate;
            }
            set {
                _itemTemplate = value;
            }
        }
        private ITemplate _itemTemplate;
        #endregion
        #region public ITemplate FirstItemTemplate
        /// <summary>
        /// Get/Sets the FirstItemTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate FirstItemTemplate {
            get {
                return _firstItemTemplate;
            }
            set {
                _firstItemTemplate = value;
            }
        }
        private ITemplate _firstItemTemplate;
        #endregion
        #region public ITemplate EmptyItemTemplate
        /// <summary>
        /// Get/Sets the EmptyItemTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate EmptyItemTemplate {
            get {
                return _emptyItemTemplate;
            }
            set {
                _emptyItemTemplate = value;
            }
        }
        private ITemplate _emptyItemTemplate;
        #endregion
        #region public ITemplate FooterTemplate
        /// <summary>
        /// Get/Sets the FooterTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate FooterTemplate {
            get {
                return _footerTemplate;
            }
            set {
                _footerTemplate = value;
            }
        }
        private ITemplate _footerTemplate;
        #endregion
        #region public int PageSize
        /// <summary>
        /// Get/Sets the PageSize of the PagedNewsList
        /// </summary>
        /// <value></value>
        public int PageSize {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        private int _pageSize;
        #endregion
        #region public bool AllowPaging
        /// <summary>
        /// Gets the AllowPaging of the PagedList
        /// </summary>
        /// <value></value>
        public bool AllowPaging {
            get { return _allowPaging; }
            set { _allowPaging = value; }
        }
        private bool _allowPaging;
        #endregion
        #region public string ResultType
        /// <summary>
        /// Get/Sets the ResultType of the PagedList
        /// </summary>
        /// <value></value>
        public string ResultType {
            get { return _resultType; }
            set { _resultType = value; }
        }
        private string _resultType;
        #endregion
        #region public PagedDataSource PagedSource
        /// <summary>
        /// Get/Sets the PagedSource of the PagedNewsList
        /// </summary>
        /// <value></value>
        public PagedDataSource PagedSource {
            get {
                if (_pagedSource == null) {
                    _pagedSource = new PagedDataSource {
                                                           DataSource = GetPages(),
                                                           AllowPaging = true,
                                                           PageSize = _pageSize
                                                       };
                }
                return _pagedSource;
            }
        }
        private PagedDataSource _pagedSource;
        #endregion
        #region private int CurrentPageIndex
        /// <summary>
        /// Gets the CurrentPageIndex of the NewsList
        /// </summary>
        /// <value></value>
        private int CurrentPageIndex {
            get {
                if (HttpContext.Current.Request.QueryString["page"] != null) {
                    int index = (Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1);
                    // correct for a page index less than 0 or greater then last page
                    if (index < 0)
                        index = 0;
                    if (index > PagedSource.PageCount)
                        index = (PagedSource.PageCount - 1);
                    return index;
                }
                return 0;
            }
        }
        #endregion
        #region public new PagingControl PagingControl
        /// <summary>
        /// Gets the PagingControl of the PagedNewsList
        /// </summary>
        /// <value></value>
        public new PagingControl PagingControl {
            get {
                if (_pagingControl == null)
                    _pagingControl = new PagingControl(PagedSource) { ResultText = ResultType };
                return _pagingControl;
            }
        }
        private PagingControl _pagingControl;
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region protected override void CreateChildControls()
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls() {
            PagedSource.CurrentPageIndex = CurrentPageIndex;

            IEnumerator e = PagedSource.GetEnumerator();

            PageDataCollection pages = new PageDataCollection();

            while (e.MoveNext()) {
                pages.Add(e.Current as PageData);
            }

            Control template = new PageTemplateContainer(null);
            // Abort if no data
            if (pages.Count == 0) {
                if (_emptyItemTemplate != null) {
                    _emptyItemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                }
                return;
            }

            template = new PageTemplateContainer(null);
            if (_headerTemplate != null) {
                _headerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }

            for (int i = 0; i < pages.Count; i++) {
                template = new PageTemplateContainer(pages[i]);
                if (_firstItemTemplate != null && i == 0) {
                    _firstItemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                } else if (_itemTemplate != null) {
                    _itemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                }
            }
            if (_footerTemplate != null) {
                template = new PageTemplateContainer(null);
                _footerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }

            // Add paginator
            if (AllowPaging)
                Controls.Add(PagingControl);
        }
        #endregion

        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
    }
}